import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'
import { getCookie } from '../utils/cookie'
Vue.use(Router)

// 初始化加载路由
import Notfound from '@/views/Notfound'  // 404
import Developing from '@/views/Developing' //开发中
import Login from '@/views/user/Login'  // 登录页
import Regist from '@/views/user/Regist' // 注册页

// 按需加载路由
const router = new Router({
  mode: 'history',  // 考虑到 后面在微信分享,授权登录等  所以用 history
  routes: [
    {
      path: '/',
      redirect: { path: '/home' }
    },

    // ------------------------首页------------------------------------------------
    {
      path: '/home',
      name: 'home',
      //路由懒加载 1 vue异步组件技术 ==== 异步加载  resolve
      // component: resolve => require(['@/views/Login'], resolve),
      //路由懒加载 2 使用 import 官网方法
      component: () => import('@/views/home/Home'),
      // 表示进入该路由需要登陆   哪里需要就在哪里加入此字段requireLogin  例:  下面的  意见反馈路由
      // meta: { requireLogin: true },
      meta: { footShow: true }, // 控制底部 tabar
    },

    // ------------------------阅读训练------------------------------------------------
    {
      path: '/readtype/:roleid',   // 选择题目类型
      name: 'readtype',
      component: () => import('@/views/home/reading/Readtype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/deepread',   // 深度阅读
      name: 'deepread',
      component: () => import('@/views/home/reading/deepread/Deepread'),
    },
    {
      path: '/reportread',   // 深度阅读(练习报告)
      name: 'reportread',
      component: () => import('@/views/home/reading/deepread/Reportread'),
    },
    {
      path: '/choosefill',   // 选词填空
      name: 'choosefill',
      component: () => import('@/views/home/reading/choosefill/Choosefill'),
    },
    {
      path: '/reportfill',   // 选词填空(练习报告)
      name: 'reportfill',
      component: () => import('@/views/home/reading/choosefill/Reportfill'),
    },
    {
      path: '/readmatch',   // 阅读匹配
      name: 'readmatch',
      component: () => import('@/views/home/reading/readmatch/Readmatch'),
    },
    {
      path: '/reportmatch',   // 阅读匹配(练习报告)
      name: 'reportmatch',
      component: () => import('@/views/home/reading/readmatch/Reportmatch'),
    },
    {
      path: '/readanswer',   // 阅读简答
      name: 'readanswer',
      component: () => import('@/views/home/reading/readanswer/Readanswer'),
    },
    {
      path: '/reportanswer',   // 阅读简答(练习报告)
      name: 'reportanswer',
      component: () => import('@/views/home/reading/readanswer/Reportanswer'),
    },
    {
      path: '/readfill',   // 阅读填空
      name: 'readfill',
      component: () => import('@/views/home/reading/readfill/Readfill'),
    },
    {
      path: '/reportblanks',   // 阅读填空(练习报告)
      name: 'reportblanks',
      component: () => import('@/views/home/reading/readfill/Reportblanks'),
    },
    {
      path: '/sectionmatch',   // 段落匹配
      name: 'sectionmatch',
      component: () => import('@/views/home/reading/sectionmatch/Sectionmatch'),
    },
    {
      path: '/reportsection',   // 段落匹配(练习报告)
      name: 'reportsection',
      component: () => import('@/views/home/reading/sectionmatch/Reportsection'),
    },

    // ------------------------听力理解------------------------------------------------
    {
      path: '/listentype/:roleid',   // 选择题目类型
      name: 'listentype',
      component: () => import('@/views/home/listening/Listentype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/hearing',   // 听力题
      name: 'hearing',
      component: () => import('@/views/home/listening/hearing/Hearing'),
    },
    {
      path: '/reporthear',   // 听力题(练习报告)
      name: 'reporthear',
      component: () => import('@/views/home/listening/hearing/Reporthear'),
    },
    {
      path: '/hearanswer',   // 听力简答
      name: 'hearanswer',
      component: () => import('@/views/home/listening/hearanswer/Hearanswer'),
    },
    {
      path: '/reportreply',   // 听力简答(练习报告)
      name: 'reportreply',
      component: () => import('@/views/home/listening/hearanswer/Reportreply'),
    },
    {
      path: '/hearfill',   // 听力填空
      name: 'hearfill',
      component: () => import('@/views/home/listening/hearfill/Hearfill'),
    },
    {
      path: '/reportlisten',   // 听力填空(练习报告)
      name: 'reportlisten',
      component: () => import('@/views/home/listening/hearfill/Reportlisten'),
    },


    // ------------------------口语训练------------------------------------------------
    {
      path: '/langugetype/:roleid',   // 选择题目类型
      name: 'langugetype',
      component: () => import('@/views/home/langugeing/Langugetype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/langugeing',   // 口语题
      name: 'langugeing',
      component: () => import('@/views/home/langugeing/Languger'),
    },


    // ------------------------写作训练------------------------------------------------
    {
      path: '/writtype/:roleid',   // 选择题目类型
      name: 'writtype',
      component: () => import('@/views/home/writing/Writtype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/writing',   // 写作训练
      name: 'writing',
      component: () => import('@/views/home/writing/Writing'),
    },


    // ------------------------翻译训练------------------------------------------------
    {
      path: '/translatetype/:roleid',   // 选择题目类型
      name: 'translatetype',
      component: () => import('@/views/home/translateing/Translatetype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/translating',   // 翻译训练
      name: 'translating',
      component: () => import('@/views/home/translateing/Translating'),
    },


    // ------------------------完形填空训练------------------------------------------------
    {
      path: '/clozetype/:roleid',   // 选择题目类型
      name: 'clozetype',
      component: () => import('@/views/home/clozeing/Clozetype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/clozing',   // 翻译训练
      name: 'clozing',
      component: () => import('@/views/home/clozeing/Clozing'),
    },


    // ------------------------词汇训练------------------------------------------------
    {
      path: '/vocabularytype/:roleid',   // 选择题目类型
      name: 'vocabularytype',
      component: () => import('@/views/home/vocabularying/Vocabularytype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/vocabularying',   // 翻译训练
      name: 'vocabularying',
      component: () => import('@/views/home/vocabularying/Vocabularying'),
    },


    // ------------------------语法训练------------------------------------------------
    {
      path: '/grammartype/:roleid',   // 选择题目类型
      name: 'grammartype',
      component: () => import('@/views/home/grammaring/Grammartype'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/grammaring',   // 翻译训练
      name: 'grammaring',
      component: () => import('@/views/home/grammaring/Grammaring'),
    },



    // -----------------------信息页-----------------------------------------------------
    {
      path: '/message',   // 信息页
      name: 'message',
      component: () => import('@/views/message/Message'),
      meta: { footShow: true }, // 控制底部 tabar
    },
    {
      path: '/message/:id',   // 信息页详情
      name: 'msgdetail',
      component: () => import('@/views/message/Msgdetail')
    },


    // -----------------------个人中心---------------------------------------------------
    {
      path: '/user',   // 个人中心首页
      name: 'user',
      component: () => import('@/views/user/User'),
      meta: { footShow: true }, // 控制底部 tabar
    },
    {
      path: '/login',  // 登录页
      name: 'login',
      component: Login,
    },
    {
      path: '/regist',   // 注册页
      name: 'regist',
      component: Regist,
    },
    {
      path: '/password',   // 修改密码
      name: 'password',
      component: () => import('@/views/user/Password'),
    },
    {
      path: '/feedback',   // 意见反馈
      name: 'feedback',
      component: () => import('@/views/user/Feedback'),
      meta: { requireLogin: true }, // 表示进入该路由需要登陆
    },
    {
      path: '/notes',   // 复习笔记
      name: 'notes',
      component: () => import('@/views/user/Notes'),
    },
    {
      path: '/personalrepair',   // 个人信息修改
      name: 'personalrepair',
      component: () => import('@/views/user/Personalrepair'),
    },

    {
      path: '/develop',   // 开发中
      name: 'develop',
      component: Developing,
    },

    {
      path: '*',   // 404
      name: 'notfound',
      component: Notfound,
    },

  ]
});

// 解决Vue-Router 路由守卫跳转报错问题 问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requireLogin)) {   //这里的requireLogin为路由中定义的
    // if (store.getters.token) {  后台接口没有token 用下面的cookie判断
    if (getCookie('jsessionid')) {
      next()
    } else {
      store.dispatch('setSignOut')  // 如果cookie过期 退出登录
      next({ path: '/login' })
    }
  } else {
    next()
  }
});

export default router
